﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.Base.Util;
using AAA.TradeLanguage;

namespace AAA.Trade
{
    public class SymbolCodeHelper
    {
        public const string FUTURES_BIG = "台指";
        public const string FUTURES_SMALL = "小台";
        public const string OPTIONS = "選擇權";
        public const string OPTIONS_PUT = "賣權";
        public const string OPTIONS_CALL = "買權";

        public const string OPTIONS_PUT_WEEK = "賣權(週)";
        public const string OPTIONS_CALL_WEEK = "買權(週)";

        private static readonly string MONTH = "ABCDEFGHIJKLMNOPQRSTUVWX";

        public static string YearMonthToTradeCode(string strPutOrCall, string strYear, string strMonth)
        {
            int iMonth = NumericHelper.ToInt(strMonth);
            string strMonthYear = "";

            switch (strPutOrCall)
            {
                case "P":
                    strMonthYear = MONTH.Substring(NumericHelper.ToInt(strMonth) - 1 + 12, 1) + strYear.Substring(strYear.Length - 1, 1);
                    break;
                default:
                    strMonthYear = MONTH.Substring(NumericHelper.ToInt(strMonth) - 1, 1) + strYear.Substring(strYear.Length - 1, 1);
                    break;
            }
            return strMonthYear;
        }

        public static string QuerySymbolCode(string strSymbolType, string strStrikePrice, string strPutOrCall, string strYear, string strMonth)
        {
            return QuerySymbolCode(strSymbolType, strStrikePrice, strPutOrCall, strYear, strMonth, "01");
        }
        public static string QuerySymbolCode(string strSymbolType, string strStrikePrice, string strPutOrCall, string strYear, string strMonth, string strDay)
        {
            string strSymbolCode = "";
            string strMonthYear = YearMonthToTradeCode(strPutOrCall, strYear, strMonth);

            switch (strSymbolType)
            {
                case FUTURES_BIG:
                    strSymbolCode = "TXF" + strMonthYear;
                    break;
                case FUTURES_SMALL:
                    strSymbolCode = "MXF" + strMonthYear;
                    break;
                case OPTIONS:
                case OPTIONS_CALL:
                case OPTIONS_PUT:
                    strSymbolCode = "TXO" + StringHelper.FillChar(strStrikePrice, '0', 5, StringFillTypeEnum.Left) + strMonthYear;
                    break;

                case OPTIONS_CALL_WEEK:
                case OPTIONS_PUT_WEEK:
                    DateTime dtQuery = new DateTime(int.Parse(strYear), int.Parse(strMonth), int.Parse(strDay));
                    DateTime dtSettle = SymbolUtil.GetWeekOptionsSettleDate(dtQuery);
                    int iWeek = SymbolUtil.GetSettleWeekNumber(dtSettle);
                    strMonthYear = YearMonthToTradeCode(strPutOrCall, dtSettle.Year.ToString(), dtSettle.Month.ToString());

                    strSymbolCode = "TX" + iWeek + StringHelper.FillChar(strStrikePrice, '0', 5, StringFillTypeEnum.Left) + strMonthYear;
                    break;
            }

            return strSymbolCode;
        }
    }
}
